package com.fxc.crowd.mvc.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;

// 表示当前类是一个配置类
@Configuration

// 启用web环境下权限控制功能
@EnableWebSecurity

// 启用全局方法权限控制功能，并且设置prePostEnabled = true.保证@Preauthority,@PostAuthority,@preFilter,@PostFilter生效
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebAppSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

//    @Bean 无法使用
//    public BCryptPasswordEncoder getPasswordEncode(){
//        return new BCryptPasswordEncoder();
//    }

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    protected void configure(AuthenticationManagerBuilder builder) throws Exception {

        // 临时使用内存版登录的模式测试代码
        //builder.inMemoryAuthentication().withUser("tom").password("123").roles("ADMIN");

        // 正式功能中使用基于数据库认证
        builder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);

    }

    @Override
    protected void configure(HttpSecurity security) throws Exception {

        security
                // 对请求进行授权                           针对登录页进行设置
                .authorizeRequests().antMatchers("/admin/to/login/page.html")
                // 无条件访问
                .permitAll()
                .antMatchers("/bootstrap/**").permitAll()
                .antMatchers("/css/**").permitAll()
                .antMatchers("/fonts/**").permitAll()
                .antMatchers("/img/**").permitAll()
                .antMatchers("/jquery/**").permitAll()
                .antMatchers("/layer/**").permitAll()
                .antMatchers("/script/**").permitAll()
                .antMatchers("/ztree/**").permitAll()
                .antMatchers("/index.jsp").permitAll()
                .antMatchers("/send/array/one.html").permitAll()
                .antMatchers("/send/array/two.html").permitAll()
                .antMatchers("/send/array/three.html").permitAll()
                .antMatchers("/send/compose/object.json").permitAll()
                .antMatchers("/admin/get/page.html") //争对分页显示Admin数据设定访问控制
                //.hasRole("经理")  //要求具备经理角色
                .access("hasRole('经理') OR hasAuthority('user:get')")
                .anyRequest()   // 其他任意的请求
                .authenticated()    // 认证后访问
                .and()
                .csrf() // 防跨站请求伪造功能
                .disable()  // 禁用
                .formLogin()    // 开启表单登录功能
                .loginPage("/admin/to/login/page.html") // 指定登录页面
                .loginProcessingUrl("/security/do/login.html")  //指定处理登录请求地址
                .defaultSuccessUrl("/admin/to/main/page.html")  // 指定登录成功后前往的地址
                .usernameParameter("loginAcct") // 账号的请求参数名称
                .passwordParameter("userPswd")  // 密码的请求参数名称
                .and()
                .logout()   // 开启退出登录功能
                .logoutUrl("/security/do/logout.html")  // 指定退出登录地址
                .logoutSuccessUrl("/admin/to/login/page.html")   // 指定退出成功后前往的地址
        ;
    }
}
